$(function () {
    fillTeamOption();
    fillStatusOption();
    getSignRecordList();
})
//为当前显示条数文本框绑定回车事件
$('#size').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        //敲击回车后重新按照新的显示条数加载页面
        getSignRecordList();
    }
});

//为当前页数文本框绑定回车事件
$('#pageipt').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        getSignRecordList();
    }
});
var total = 0;
var index = 1;
var pageSize = 5;
//为上一页绑定点击事件
$('#lastpage').click(function () {
    //获取默认的当前页数和显示条数值
    let page = $("#pageipt").val();
    page--;
    if (page >= index) {//如果不是第一页，则往前翻一页
        $("#pageipt").val(page);
        getSignRecordList();
    }
});

//绑定下一页点击事件
$('#nextpage').click(function () {
    //获取默认的当前页数和显示条数值
    let page = $("#pageipt").val();
    let size = $("#size").val();
    page++;
    if (page <= Math.ceil(total / size)) {
        $("#pageipt").val(page);
        getSignRecordList();
    }
});

//为首页绑定点击事件
$("#first").click(function () {
    let page = $("#pageipt").val();
    if (page > index) {//如果当前页不是首页则跳转到首页，否则不做任何事情
        page = index;
        $("#pageipt").val(page);
        getSignRecordList();
    }
});

//为尾页绑定点击事件
$("#last").click(function () {
    let page = $("#pageipt").val();
    let size = $("#size").val();
    if (page < Math.ceil(total / size)) {//如果当前页不是尾页，则跳转到尾页
        page = Math.ceil(total / size);
        $("#pageipt").val(page);
        getSignRecordList();
    }
});

function getSignRecordList() {
    let param = $("#search_ipt").val();
    let start_date = $("#start_date").val();
    let end_date = $("#end_date").val();
    let team = $("#team_select").find("option:selected").val();
    let status = $("#status_select").find("option:selected").val();
    let sort = $("#sort").find("option:selected").val();
    let page = $("#pageipt").val();
    let size = $("#size").val();
    if (page.trim() == '' || parseInt(page.trim()) < index)
        page = index;
    if (size.trim() == '' || parseInt(size.trim()) < index)
        size = pageSize;
    let map = {
        "param": param,
        "start_date": start_date,
        "end_date": end_date,
        "team": team,
        "status": status,
        "sort": sort,
        "page": page,
        "size": size
    };
    let data = myAjax("/sign/getSignRecord", "get", map, "json");
    console.log(data);
    $("tbody").html("");
    $.each(data.list, function (i, sign) {
        $("tbody").append("<tr><td><input type='checkbox' value='"+sign.id+"'></td><td>" + sign.id + "</td><td>" + sign.user.name + "</td>" +
            "<td>" + sign.signTime + "</td><td>" + sign.status.info + "</td><td>" + (sign.lateTime == null ? "未迟到" : sign.lateTime) + "</td>" +
            "<td>" + sign.status.name + "</td><td>" + sign.createTime + "</td>" +
            "<td>" + (sign.updateTime == null ? "暂无更新" : sign.updateTime) + "</td><td><button class='iconfont'>&#xe608;</button></td></tr>");
    });
    total = data.total;
    //显示总条数
    $("#pagecount").text(total);
    console.log(total);
}

function deleteSelected() {
    let signid = new Array();
    $("input[type=checkbox]:checked").each(function (key, value) {
        if ($(this).val() != "on")
            signid.push(parseInt($(this).val()));
    });
    console.log(signid);
    let data = myAjax("/sign/deleteSigns","post",{"arrays":JSON.stringify(signid)},"json");
    if (data==200)
        alert("删除成功");
    else if (data==500)
        alert("后台错误");
    else
        alert("404未找到");
    getSignRecordList();
}

function fillTeamOption() {
    let data = myAjax("/team/getAllTeam", "get", {}, "json");
    $("#team_select").html("<option value='全部'>全部</option>");
    $.each(data, function (i, team) {
        $("#team_select").append("<option value='" + team.code + "'>" + team.name + "</option>");
    })
}

function fillStatusOption() {
    let data = myAjax("/data/getSignStatus", "get", {}, "json");
    $("#status_select").html("<option value='全部'>全部</option>");
    $.each(data, function (i, status) {
        $("#status_select").append("<option value='" + status.code + "'>" + status.name + "</option>");
    })
}